######################################################################
# Time-stamp: <05/07/20 15:50:12 ostolop>
#
# Core Web Services
######################################################################
package EP::WS::Core;
use strict;

use EP::Common::DBI::Dataset;

# sub: add_new_experimental_factor
#   adds a new EF to a dataset
# params:
#   dataset_id - id of dataset to which EF should be added
#   factor - name of new factor
#   fvs - ref to array of new factor values, should correspond to the number of columns in dataset (dataset_id)
sub add_experimental_factor {
  my $self = shift;
  my ($dataset_id, $factor, $fvs ) = @_;

  my $dataset = retrieve EP::Common::DBI::Dataset ( $dataset_id );
  die "Failed to find dataset with id $dataset_id!" if not $dataset;

  my $ds_type = $dataset->type;
  die "Cannot add experimental factors to dataset of type $ds_type!" unless $ds_type eq "exp";

  my $data = $dataset->data->add_experimental_factor ( $factor, $fvs );
  return "OK";
}

# sub: delete_new_experimental_factor
#   deletes EF from a dataset
# params:
#   dataset_id - id of dataset to which EF should be added
#   factor - name of new factor
sub delete_experimental_factor {
  my $self = shift;
  my ($dataset_id, $factor ) = @_;

  my $dataset = retrieve EP::Common::DBI::Dataset ( $dataset_id );
  die "Failed to find dataset with id $dataset_id!" if not $dataset;

  my $ds_type = $dataset->type;
  die "Cannot delete experimental factors from dataset of type $ds_type!" unless $ds_type eq "exp";

  my $data = $dataset->data->delete_experimental_factor ( $factor );
  return "OK";
}

# sub: identify_annotations
#   (re-)identifies row annotation columns for a dataset
# params:
#   dataset_id - id of dataset for which the row annots are to be identified
#   identannots - array ref of new annot identifiers to replace the existing ones
sub identify_annotations {
  my $self = shift;
  my ($dataset_id, $identannots ) = @_;

  my $dataset = retrieve EP::Common::DBI::Dataset ( $dataset_id );
  die "Failed to find dataset with id $dataset_id!" if not $dataset;

  my $ds_type = $dataset->type;
  die "Cannot identify annotations for datasets of type $ds_type!" unless $ds_type eq "exp";

  my $data = $dataset->data->identify_annotations ( $identannots );
  return "OK";
}

1;
